using System;
using System.Collections.Generic;
using com.yoozoo.gta.Gameplay.City.AI.FreeAISystem;
using Neonagee.LocalPreferences;
using Unity.Collections;
using Unity.Jobs;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.Profiling;
namespace com.yoozoo.gta.Gameplay.City.AI
{
    public class FreeAIDataManager
    {
        private static FreeAIDataManager m_instance;

        public static bool Enabled = true;

        /// <summary>
        /// 0 正常update
        /// 1 下一帧停止update
        /// 2 停止update
        /// </summary>
        private int updateState = 0;

        public FreeAINpcNativeData npcNativeData;
        
        public List<FreeNpcObject> FreeNpcObjectList = new List<FreeNpcObject>();
        
        
        private bool _isDisposed = false;
        
        private bool freeNpcDataReady = false;

        public static FreeAIDataManager Instance
        {
            get
            {
                if (m_instance == null)
                {
                    m_instance = new FreeAIDataManager();
                }

                return m_instance;
            }
        }

        // 初始化自由npc数据
        public void InitFreeNpc(int index)
        {
            FreeNpcObject npc = FreeAIResourceManager.CreateFreeNpc();
            FreeNpcObjectList[index] = npc;
        }
        
        private void ExecuteJob(float realTime)
        {
            //Profiler.BeginSample("[C#]FreeAIDataManager.ExecuteJob");
            FreeAIJob jobData = new FreeAIJob();
            
            jobData.npcData = npcNativeData;

            //JobHandle handle = jobData.Schedule(1,1);
            //handle.Complete();
            //Profiler.EndSample();
            
            // if (isSpriteJobs)
            // {
            //     animSpriteJobs.Clear();
            //     for (int i = 0; i < animSpriteData.Count; i++)
            //     {
            //         if (animSpriteJobs.Count <= i)
            //         {
            //             animSpriteJobs.Add(new AnimSpriteJob
            //             {
            //                 childData = childNativeData,
            //                 ps = animSpriteData[i],
            //             });
            //         }
            //         jobHandles.Add(animSpriteJobs[i].Schedule());
            //         //handle2.Complete();
            //     }
            //
            //     for (int i = 0; i < jobHandles.Count; i++)
            //     {
            //         jobHandles[i].Complete();
            //     }
            //     jobHandles.Clear();
            // }
            // else
            // {
            //     for (int i = 0; i < childNativeData.count; i++)
            //     {
            //         if (childNativeData.valid[i])
            //         {
            //             var data = childNativeData.animSpriteData[i];
            //             var sprite = animSprites[i];
            //             if (sprite != null)
            //             {
            //                 sprite.position = data.position;
            //                 sprite.rotation = data.forward;
            //                 if (data.changeAnimationFlag)
            //                 {
            //                     sprite.action = (SPRITE_ACTION_ENUM)data.animationIndex;
            //                 }
            //                 if (data.changeAlphaFlag)
            //                 {
            //                     sprite.alpha = data.alpha;
            //                 }
            //
            //                 sprite.speed = data.animationSpeed;
            //             }
            //             
            //             data.changeAnimationFlag = false;
            //             
            //             childNativeData.animSpriteData[i] = data;
            //         }
            //     }
            // }
        }

        public void Update(float realTime,float deltaTime)
        {
            if (!Enabled)
            {
                return;
            }
            
            if (updateState == 1)
            {
                updateState = 2;
            }
            else if (updateState == 2)
            {
                return;
            }
            ExecuteJob(realTime);

        }

        public void Dispose()
        {
            if (_isDisposed)
            {
                return;
            }
            _isDisposed = true;
        }
    }
}